import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { h } from 'vue';
import { Icon } from '/@/components/Icon';
import moment from 'moment';

export const columns: BasicColumn[] = [
  {
    title: '菜单名称',
    dataIndex: 'name',
    width: 200,
    align: 'left',
  },
  {
    title: 'id',
    dataIndex: 'id',
  },
  {
    title: '图标',
    dataIndex: 'icon',
    width: 50,
    customRender: ({ record }) => {
      return h(Icon, { icon: record.icon });
    },
  },
  {
    title: '组件',
    dataIndex: 'component',
  },
  {
    title: '组件名',
    dataIndex: 'componentName',
  },
  {
    title: '排序',
    dataIndex: 'sort',
    width: 50,
  },
  {
    title: '菜单类型',
    dataIndex: 'type',
    customRender: ({ record }) => {
      const type: number = record.type;
      switch (type) {
        case 1:
          return '目录';
          break;
        case 2:
          return '菜单';
          break;
        case 3:
          return '按钮';
          break;
      }
    },
  },
  {
    title: '权限标识',
    dataIndex: 'permission',
  },
  {
    title: '路由地址',
    dataIndex: 'path',
  },
  {
    title: '状态',
    dataIndex: 'status',
    customRender: ({ record }) => {
      switch (record.status) {
        case 0:
          return '启用';
          break;
        case 1:
          return '停用';
          break;
      }
    },
  },
  {
    title: '是否可见',
    dataIndex: 'visible',
    customRender: ({ record }) => {
      switch (record.visible) {
        case true:
          return '是';
          break;
        case false:
          return '否';
          break;
      }
    },
  },
  {
    title: '是否总是显示',
    dataIndex: 'alwaysShow',
    customRender: ({ record }) => {
      switch (record.alwaysShow) {
        case true:
          return '是';
          break;
        case false:
          return '否';
          break;
      }
    },
  },
  {
    title: '是否缓存',
    dataIndex: 'keepAlive',
    customRender: ({ record }) => {
      switch (record.keepAlive) {
        case true:
          return '是';
          break;
        case false:
          return '否';
          break;
      }
    },
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    customRender: ({ record }) => {
      const time = record.createTime;
      return time ? moment(time).format('YYYY-MM-DD HH:mm:ss') : '-';
    },
  },
];
const isDir = (type: number) => type === 1;
const isMenu = (type: number) => type === 2;
const isButton = (type: number) => type === 3;

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '菜单名称',
    component: 'Input',
    colProps: { span: 8 },
    componentProps: ({ tableAction }) => {
      return {
        onPressEnter: () => {
          const { reload } = tableAction;
          reload();
        },
      };
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    colProps: { span: 8 },
    componentProps: {
      options: [
        {
          label: '启用',
          value: 0,
        },
        {
          label: '禁用',
          value: 1,
        },
      ],
    },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'type',
    label: '菜单类型',
    component: 'RadioButtonGroup',
    defaultValue: 1,
    componentProps: {
      options: [
        { label: '目录', value: 1 },
        { label: '菜单', value: 2 },
        { label: '按钮', value: 3 },
      ],
    },
    colProps: { lg: 24, md: 24 },
    required: true,
  },
  {
    field: 'parentId',
    label: '上级菜单',
    component: 'TreeSelect',
    componentProps: {
      placeholder: ' ',
      replaceFields: {
        title: 'name',
        key: 'id',
        value: 'id',
      },
      getPopupContainer: () => document.body,
    },
    // required: true,
  },
  {
    field: 'name',
    label: '菜单名称',
    component: 'Input',
    required: true,
  },
  {
    field: 'permission',
    label: '权限标识',
    component: 'Input',
    ifShow: ({ values }) => !isDir(values.type),
  },
  {
    field: 'path',
    label: '路由地址',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'component',
    label: '组件路径',
    component: 'Input',
    required: true,
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'componentName',
    label: '组件名',
    component: 'Input',
    ifShow: ({ values }) => !isDir(values.type),
  },
  {
    field: 'redirect',
    label: '重定向',
    component: 'Input',
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'sort',
    label: '排序',
    component: 'InputNumber',
    required: true,
  },
  {
    field: 'icon',
    label: '图标',
    component: 'IconPicker',
    ifShow: ({ values }) => !isButton(values.type),
  },
  // {
  //   field: 'perms',
  //   label: '权限标识',
  //   component: 'Input',
  //   ifShow: ({ values }) => !isDir(values.type),
  // },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '启用',
          value: 0,
        },
        {
          label: '禁用',
          value: 1,
        },
      ],
    },
    required: true,
  },
  {
    field: 'currentActiveMenu',
    label: '定向详情',
    defaultValue: null,
    component: 'Input',
    ifShow: ({ values }) => isMenu(values.type),
  },
  {
    field: 'keepAlive',
    label: '是否缓存',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '否', value: false },
        { label: '是', value: true },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'alwaysShow',
    label: '隐藏标签',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '否', value: false },
        { label: '是', value: true },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
  {
    field: 'visible',
    label: '是否隐藏',
    component: 'RadioButtonGroup',
    defaultValue: false,
    componentProps: {
      options: [
        { label: '否', value: false },
        { label: '是', value: true },
      ],
    },
    ifShow: ({ values }) => !isButton(values.type),
  },
];
